package Game;
/**
 * A StopWatch that measures elapsed time.
 * 
 * @ Parinthorn Panya 5510546085
 * @version 2013.01.23
 */
public class StopWatch {
	/** constant for converting nanoseconds to seconds. */
	private static final double NANOSECONDS = 1.0E-9;
	/** time when stopwatch was started. */
	private long startTime;
	/** time when stopwatch was stopped. */
	private long stopTime;
	/** flag to indicate whether stopwatch is running. */
	private boolean running = false;

	/**
	 * Constructor initializes a new stopwatch.
	 */
	public StopWatch() {
		this.startTime = 0;
		this.stopTime = 0;
	}

	/**
	 * Start the stopwatch. If it is already running then this method does
	 * nothing.
	 */
	public void start() {
		if (!running) {
			startTime = System.nanoTime();
			running = true;
		}
	}

	/**
	 * Stop the stopwatch.
	 */
	public void stop() {
		if (running) {
			stopTime = System.nanoTime();
			running = false;
		}
	}

	/**
	 * To check the stopwatch is running or not.
	 * 
	 * @return true if stopwatch start and false if stopwatch stop.
	 */
	public boolean isRunning() {
		return running;
	}

	/**
	 * Get the elapsed.
	 * 
	 * @return time at now when stopwatch is running .If not return the elapsed.
	 */
	public double getElapsed() {
		if (running)
			return (System.nanoTime() - startTime) * NANOSECONDS;
		return (stopTime - startTime) * NANOSECONDS;
	}

}
